package io.renren.modules.generator.service.impl;

import io.renren.constant.DeviceFinal;
import io.renren.modules.generator.dao.DeviceSiteDao;
import io.renren.modules.generator.dao.DeviceWarningQuotaDao;
import io.renren.modules.generator.dto.GardenPortraitCommonResult;
import io.renren.modules.generator.dto.GardenPortraitWeekWarning;
import io.renren.modules.generator.entity.DeviceSiteEntity;
import io.renren.modules.generator.service.GardenPortraitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhangxu
 * @create 2020/9/15
 * Description:
 */
@Service("gardenPortraitService")
public class GardenPortraitServiceImpl implements GardenPortraitService {

    @Autowired
    private DeviceWarningQuotaDao deviceWarningQuotaDao;
    @Autowired
    private DeviceSiteDao deviceSiteDao;
    public String getDatePoor(Date endDate, Date startDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
    @Override
    public Map<String, Object> wariningGeneral() {
        Map<String,Object> map = new HashMap<>();
        Date startDate = new Date(DeviceFinal.runtimeStr);
        Date nowDate = new Date();
        String dateSub = getDatePoor(nowDate,startDate);
        map.put("systemRuntime",dateSub);
        map.put("warningCount",deviceWarningQuotaDao.selectCount(null));
        map.put("deviceCount",deviceSiteDao.selectCount(null));
        return map;
    }

    @Override
    public Map<String, Object> wariningPorportion() {
        Map<String,Object> map = new HashMap<>();
        Integer deviceTotal = deviceSiteDao.selectCount(null);
        Integer warningDeviceTotal = deviceWarningQuotaDao.selectWarningDeviceTotal();
        map.put("warningDeviceTotal",warningDeviceTotal);
        map.put("deviceTotal",deviceTotal);
        return map;
    }

    @Override
    public List<GardenPortraitWeekWarning> wariningWeekstatis() {
        return deviceWarningQuotaDao.selectWariningWeekstatis();
    }

    @Override
    public List<GardenPortraitCommonResult> deviceRuntime() {
        List<DeviceSiteEntity> deviceSiteEntityList = deviceSiteDao.selectList(null);
        List<GardenPortraitCommonResult> gardenPortraitCommonResultList = new ArrayList<>();
        Date nowDate = new Date();
        deviceSiteEntityList.forEach(deviceSiteEntity -> {
            GardenPortraitCommonResult gardenPortraitCommonResult = GardenPortraitCommonResult.builder()
                    .name(deviceSiteEntity.getSiteName())
                    .value(getDatePoor(nowDate,deviceSiteEntity.getCreateTime()))
                    .build();
            gardenPortraitCommonResultList.add(gardenPortraitCommonResult);
        });
        return gardenPortraitCommonResultList;
    }
}
